﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 必须是正确的二代身份证号, 示例:
/// <code>
/// [VMustIdCard]
/// public string IdCard { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustIdCardAttribute(string errorMessage = null) : Attribute, IValidateAction
{
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustIdCardInternal(ctx, ctx.ModelValue?.ToString(), ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则必须是正确的二代身份证号, 示例:
/// <code>
/// [VMustIdCardIfNotNull]
/// public string IdCard { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustIdCardIfNotNullAttribute(string errorMessage = null) : Attribute, IValidateAction
{
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateStringExtensions.MustIdCardInternal(ctx, ctx.ModelValue?.ToString(), ErrorMessage);
    }
}
